package framework.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import framework.utils.GameModuleLoader;
import framework.utils.dto.*;
import framework.controller.*;
import framework.model.*;

public class ChallengeView extends InteractiveView {

	private JLabel nameLabel, gameTypeLabel;
	private JButton acceptButton, removeButton;
	private JPanel buttonsPanel;
	private Challenge challenge;
	
	/**********************
	 *** GLOBAL METHODS ***
	 *********************/
	
	/**
	 * @method ChallengeView
	 * @param model
	 * @param controller
	 * @param challenge
	 */
	public ChallengeView(LobbyModel model, LobbyController controller, Challenge challenge) {
		super(model, controller);
		this.setChallenge(challenge);
		this.setLabels();
		this.setButtons();
		this.setButtonsPanel();
		this.initialize();
	};
	
	/**
	 * Initialize view panel
	 * @method initialize
	 */
	private void initialize() {
		super.setLayout(new BorderLayout(5,5));
		super.setPreferredSize(new Dimension(600,80));
		super.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		super.add(this.nameLabel, BorderLayout.NORTH);
		super.add(this.gameTypeLabel, BorderLayout.CENTER);
		super.add(this.buttonsPanel, BorderLayout.SOUTH);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand()) {
			case "remove" :
				this.getController().handleChallengeRemoveRequest(this.challenge);
			break; 
			case "accept" :
				this.getController().handleChallengeAcceptRequest(this.challenge);
			break;
		};
	};
	
	/**********************
	 *** SETTER METHODS ***
	 *********************/
	
	/**
	 * Set labels 
	 * @method setLabels
	 */
	private void setLabels() {
		//GameType label
		this.gameTypeLabel = new JLabel(this.challenge.getGametype());
		//Name label
		this.nameLabel = new JLabel(this.challenge.getPlayername());
		this.nameLabel.setFont(new Font("Arial", Font. BOLD,14));
	};
	
	/**
	 * Set action buttons
	 * @method setButtons
	 */
	private void setButtons() {
		//Remove button
		this.removeButton = new JButton("remove");
		this.removeButton.setActionCommand("remove");
		this.removeButton.addActionListener(this);
		//Accept button
		this.acceptButton = new JButton("accept");
		this.acceptButton.setActionCommand("accept");
		this.acceptButton.addActionListener(this);
	};
	
	/**
	 * Set panel for holding buttons
	 * @method setButtonsPanel
	 */
	private void setButtonsPanel() {
		this.buttonsPanel = new JPanel(new GridLayout(1,4,10,0));
		this.buttonsPanel.add(new JLabel(""));
		this.buttonsPanel.add(new JLabel(""));
		this.buttonsPanel.add(this.removeButton);
		this.buttonsPanel.add(this.acceptButton);
	};
	
	/**
	 * Initialize challenge
	 * @method setChallenge
	 * @param <Challenge> challenge
	 */
	private void setChallenge(Challenge challenge) {
		this.challenge = challenge;		
	}
	
	/**********************
	 *** GETTER METHODS ***
	 *********************/
	
	/**
	 * @method getController
	 * @return <LobbyController> controller
	 */
	public LobbyController getController() {
		return (LobbyController) super.getController();
	};
	
	/**
	 * @method getModel
	 * @return <LobbyModel> model
	 */
	public LobbyModel getModel() {
		return (LobbyModel) super.getModel();
	};
	
};